package cn.iocoder.yudao.module.biz.controller.admin.htPlan;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.htPlanDetail.vo.HtPlanDetailJoinPageVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.htPlan.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlan.HtPlanDO;
import cn.iocoder.yudao.module.biz.convert.htPlan.HtPlanConvert;
import cn.iocoder.yudao.module.biz.service.htPlan.HtPlanService;

@Api(tags = "管理后台 - 隐患排查计划")
@RestController
@RequestMapping("/biz/ht-plan")
@Validated
public class HtPlanController {

    @Resource
    private HtPlanService htPlanService;

    @PostMapping("/create")
    @ApiOperation("创建隐患排查计划")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:create')")
    public CommonResult<String> createHtPlan(@Valid @RequestBody HtPlanCreateReqVO createReqVO) {
        return success(htPlanService.createHtPlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新隐患排查计划")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:update')")
    public CommonResult<Boolean> updateHtPlan(@Valid @RequestBody HtPlanUpdateReqVO updateReqVO) {
        htPlanService.updateHtPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除隐患排查计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:delete')")
    public CommonResult<Boolean> deleteHtPlan(@RequestParam("id") String id) {
        htPlanService.deleteHtPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得隐患排查计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:query')")
    public CommonResult<HtPlanRespVO> getHtPlan(@RequestParam("id") String id) {
        HtPlanDO plan=htPlanService.getHtPlan(id);
        List<HtPlanRiskVO> riskList = htPlanService.queryRiskByPlan(id);
        HtPlanRespVO respVO=HtPlanConvert.INSTANCE.convert(plan);
        respVO.setRiskList(riskList);
        return success(respVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得隐患排查计划列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:query')")
    public CommonResult<List<HtPlanRespVO>> getHtPlanList(@RequestParam("ids") Collection<String> ids) {
        List<HtPlanDO> list = htPlanService.getHtPlanList(ids);
        return success(HtPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得隐患排查计划分页")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:query')")
    public CommonResult<PageResult<HtPlanJoinPageVO>> getHtPlanPage(@Valid HtPlanPageReqVO pageVO) {
        PageResult<HtPlanJoinPageVO> result=new PageResult<>();
        int pageNo=pageVO.getPageNo();
        pageVO.setPageNo((pageNo-1)*pageVO.getPageSize());

        //登录用户ID
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        pageVO.setCreator(userId);

        List<HtPlanJoinPageVO> list = htPlanService.selectHtPlanPage(pageVO);
        Integer count= htPlanService.selectHtPlanPageCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return success(result);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出隐患排查计划 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportHtPlanExcel(@Valid HtPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HtPlanDO> list = htPlanService.getHtPlanList(exportReqVO);
        // 导出 Excel
        List<HtPlanExcelVO> datas = HtPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "隐患排查计划.xls", "数据", HtPlanExcelVO.class, datas);
    }

}
